/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart.connection;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;

/**
 * Common ancestor of ConnectionsEditPolicy.
 * Provide miscelaneous methods to compute anchors positions.
 * 
 * @author dumoulin
 */
public abstract class ConnectionsEditPolicy extends GraphicalNodeEditPolicy {
	
	/**
	 * 
	 */
	public ConnectionsEditPolicy() {
		super();
	}
	
	/**
	 * Compute the anchor location for the provided position and editPart.
	 * 
	 * @param editPart 
	 * @param reference 
	 * 
	 * @return 
	 */
	protected Point computeAnchorLocation(Point reference, EditPart editPart) {
//		First, translate the reference to the coordinate of the diagram (like shape constraints).
		IFigure parentFigure = ((GraphicalEditPart)editPart).getFigure();
		Point res = reference.getCopy();
		parentFigure.translateToRelative(res);
		if(editPart instanceof AnchorableEditPart )
			return ((AnchorableEditPart)editPart).getAnchorMngr().getRelativeAnchorPosition(res);


//		default, return the reference itself.
		return res;
	}
}
